faApp.controller('LinkListCtrl',['$scope','linkService','$location','$controller','$route',function($scope,linkService,$location,$controller,$route) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.ads = [];
  $scope.loading = true;

	$scope.$watch('config',function(){
    linkService.linkList($scope.config).then(function(json){
      $scope.ads = json.list;
      $scope.pager = json.pager;
      $scope.loading = false;
    });
  },true)

	$scope.config = {
		"pageSize" : 20,
		"pageNo" : 1
	};
  
	$scope.pageChanged = function(){
		$scope.config.pageNo = $scope.pager.pageNo;
	};

	$scope.linkDelete = function(id){
    linkService.linkDelete({id:id}).then(function(json){
      $route.reload();    
    });
	}; 
}]);

faApp.controller('LinkAddCtrl',['$scope','linkService','$location','$controller','$timeout',function($scope,linkService,$location,$controller,$timeout) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.ad = {"advertSort":10};
  $scope.loading = true; 

	$scope.save = function(){
    if ($scope.form.$invalid){
      if ($scope.form.title.$invalid)$scope.form.title.$dirty = true;
      if ($scope.form.pic.$invalid)$scope.form.pic.$dirty = true;
      if ($scope.form.link.$invalid)$scope.form.link.$dirty = true;
      if ($scope.form.startTime.$invalid)$scope.form.startTime.$dirty = true;
      if ($scope.form.endTime.$invalid)$scope.form.endTime.$dirty = true;
      if ($scope.form.sort.$invalid)$scope.form.sort.$dirty = true;
    }else{
      if ($scope.ad.startTime){$scope.ad.startTime = $scope.ad.startTime + " 00:00:00";}
      if ($scope.ad.endTime){$scope.ad.endTime = $scope.ad.endTime + " 23:59:59";}

      linkService.linkAdd($scope.ad).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/link/list");
        }
      });
    }
  };
 
}]);

faApp.controller('LinkEditCtrl',['$scope','linkService','$location','$controller','$routeParams','$filter',function($scope,linkService,$location,$controller,$routeParams,$filter) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.ad = {};
  $scope.loading = true; 
  $scope.id = $routeParams.id;

  /*
  linkService.linkQuery($scope.id).then(function(json){
    $scope.ad = json.dto;
    if ($scope.ad.startTime){ $scope.ad.startTime = $filter("date")($scope.ad.startTime,"yyyy-MM-dd"); }
    if ($scope.ad.endTime){ $scope.ad.endTime = $filter("date")($scope.ad.endTime,"yyyy-MM-dd"); }
  });
  */

  linkService.linkList().then(function(json){
    angular.forEach(json.list || [],function(o){
      if (o.id == $scope.id){
        $scope.ad = o;
      }  
    });  
  });

	$scope.save = function(){
    if ($scope.form.$invalid){
      if ($scope.form.title.$invalid)$scope.form.title.$dirty = true;
      if ($scope.form.pic.$invalid)$scope.form.pic.$dirty = true;
      if ($scope.form.link.$invalid)$scope.form.link.$dirty = true;
      if ($scope.form.startTime.$invalid)$scope.form.startTime.$dirty = true;
      if ($scope.form.endTime.$invalid)$scope.form.endTime.$dirty = true;
      if ($scope.form.sort.$invalid)$scope.form.sort.$dirty = true;
    }else{
      if ($scope.ad.startTime){$scope.ad.startTime = $scope.ad.startTime + " 00:00:00";}
      if ($scope.ad.endTime){$scope.ad.endTime = $scope.ad.endTime + " 23:59:59";}

      delete $scope.ad.color;
      linkService.linkEdit($scope.ad).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/link/list");
        }
      });
    }
  };
 
}]);

